from moviepy.editor import VideoFileClip

def convert_video_to_gif(video_path, gif_path, start_time=0, duration=None, fps=10):
    """
    Convert a video to a GIF.
    :param video_path: Path to the video file.
    :param gif_path: Path where the GIF should be saved.
    :param start_time: Start time for the GIF in seconds (default is 0).
    :param duration: Duration of the GIF in seconds (default is None, meaning the whole video).
    :param fps: Frames per second for the GIF (default is 10).
    """
    # Load the video file
    clip = VideoFileClip(video_path)

    # If a duration is given, cut the clip
    if duration:
        clip = clip.subclip(start_time, start_time + duration)
    else:
        clip = clip.subclip(start_time)

    # Reduce the fps to reduce the size of the GIF
    clip = clip.set_fps(fps)

    # Write the GIF
    clip.write_gif(gif_path)

# 使用示例
convert_video_to_gif("E:/graguate/result/003/obj_000001.avi", "E:/graguate/result/003/gif.gif", start_time=5, duration=10)
